#pragma once

#include <shlobj.h>
#include <list>
#include <vector>
#include <string>
#include <gdiplus.h>
#include "DesktopSortConfig.h"

using namespace Gdiplus;
using namespace std;

enum DesktopItemType
{
	System,
	SystemFolder,
	File,
	Folder
};

struct DesktopItem
{
	LPCITEMIDLIST itemIdList;
	DesktopItemType itemType;
	wstring filePath;
	POINT originalPosition;
};

enum DesktopWallpaperStyle
{
	BackgroundCenter,
	BackgroundTile,	
	BackgroundStretch
};

class DesktopSorter
{
public:
	DesktopSorter(IFolderView* pFolderView);
	virtual ~DesktopSorter();

	void ClearItems()
	{
		m_DesktopItems.clear();
	}

	void SetSilentMode(bool silentMode)
	{
		m_SilentMode = silentMode;
	}

	void AddDesktopItem(LPCITEMIDLIST itemIdList, DesktopItemType itemType, wstring filePath = L"");

	bool ArrangeDesktop(const DesktopSortConfig& m_DesktopSortConfig);	

private:

	DesktopSortConfig m_DesktopSortConfig;

	IFolderView* m_pFolderView;
	vector<DesktopItem> m_DesktopItems;

	// GDI Plus
	GdiplusStartupInput m_GdiPlusStartupInput;
	ULONG_PTR m_GdiPlusToken;

	Gdiplus::Font* m_TextFont;
	Gdiplus::SolidBrush m_TextBrush;
	Gdiplus::SolidBrush m_TextShadowBrush;
	Gdiplus::Pen m_BroderPen;

	wstring m_OriginalDesktop;
	bool m_IsAutoArrange;
	bool m_IsSnapToGrid;
	bool m_SilentMode;

	bool PostionItems(vector<DesktopItem>& items, int xOffset, int yOffset, int& lastX, int& lastY);
	void DrawBackgroundImage(Gdiplus::Graphics &graphics, int cx, int cy, bool useExistingWallpaper = true);
	void DrawIconsGroup(const wstring& title, int& offsetX, int& offsetY, Gdiplus::Graphics& graphics, vector<DesktopItem>& desktopItems, int& lastX, int& lastY);
	int GetEncoderClsid(const WCHAR* format, CLSID* pClsid);
	wstring GetWallpaperPath();
	void CheckAutoArrange(HWND hwnd);
	void CheckSnapToGrid(HWND hwnd);
	DesktopWallpaperStyle GetWallpaperSettings();
	void SetWallpaperSettings(const DesktopWallpaperStyle style);
	Gdiplus::Color GetDesktopBackgroudColor();
};
